<div id="main">
    <!-- 订货单开始 -->
    <div class="cart-title">
        <h1>
            <i class="icon">&#x3c;</i>购物车
        </h1>
    </div>
    <{if $is_empty}>
    <{include file="site/cart/cart_empty.html" app=b2c}>
    <{else}>
    <!-- 订货单步骤图 -->
    <div id="cart_steps" class="steps">
        <table width="100%">
            <tbody>
            <tr>
                <td class="step-item step-1 active"><span><q class="icon">&#x50;</q><i>1</i>购物车</span></td>
                <td class="icon step-arrow">&#x28;</td>
                <td class="step-item step-2"><a href="<{link app=b2c ctl=site_cart act=checkout}>"><q class="icon">&#x50;</q><i>2</i>填写和提交订单信息</a></td>
                <td class="icon step-arrow">&#x28;</td>
                <td class="step-item step-3"><span><q class="icon">&#x50;</q><i>3</i>成功提交订单</span></td>
            </tr>
            </tbody>
        </table>
    </div>
    <!-- 订货单主体 -->
    <div id="cart_container" class="cart-container">
        <{include file="site/cart/cart_main.html" app=b2c}>
    </div>
    <{/if}>

</div>

<script type="text/javascript">
    (function()
    {
    <{if !$is_empty}>
    <{if $money_format}>
        priceControl.spec = <{$money_format}>;
    <{/if}>
        var container = $('cart_container');
        var typeJSON = {
            'product': ['商品', '购买'],
            'packs': ['礼包', '购买'],
            'exchange': ['赠品', '兑换']
        };

        //= 获取商品属性值
        function getTypeValue(type) {
            if(!type) return null;
            var value = typeJSON[type];
            if(typeof value == 'string') value = [value, '购买'];
            return {name:value[0], mode: value[1]};
        }

        //////////////////////////////////////////////

        //== 商品数量输入框正确性检测
        function inputCheck(input, options) {
            if(!input) return false;
            options = options || {};
            if(isNaN(options.min)) options.min = 1;
            if(isNaN(options.max)) options.max = 9999;
            //
            options.type = options.type || 'product';
            options['default'] = options['default'] || options.min;
            //
            var info = getTypeValue(options.type);
            var value = +input.value;
            var tips = new Tips(input);
            var pre = '';
            var msg = '';
            if(options.store && options.store - value <= 0) {
                pre = '库存有限，';
            }
            if(value < options.min) {
                input.value = options.min;
                msg = '此' + info.name + '的最小' + info.mode + '数量为' + options.min + '件';
            }
            else if(value > options.max){
                input.value = options.max;
                msg = pre + '此' + info.name + '最多只能' + info.mode + options.max + '件';
            }
            else if(isNaN(value)) {
                input.value = options['default'];
                msg = '只允许输入数字';
            }
            if (msg) {
                tips.show(msg);
                return false;
            }
            tips.hide();
            //warn.hide();
            if(options.type == 'packs') {

                input.getParent('.cart-item').getElements('.p-packs-item .p-num').set('text', value);

            }
            input.value = parseInt(value);
            return true;
        }



        //add by hzk
        //////////////////////////////////////////////

        //== 获取商品数量值
        function getQuantity(el, type) {
            return el.getElement('input[name=' + type + ']').value;
        }
        //== 设置商品数量
        function setQuantity(input, value) {
            var type = input.getParent('.p-packs') ? 'packs' : input.getParent('.cart-exchange') ? 'exchange' : 'product';
            var typeName = typeJSON[type];
            var p = input.getParent('tr');

            //最小值取输入框的评论数（即输入框input的min值）//add by hzk
            var newmin=input.min;
            // alert(input.min);
            var newmax = input.max;
            //这里调用
            if(!inputCheck(input,
                    {
                        //最小值改为批量数；hzk
                        min: newmin,
                        //min: getQuantity(p, 'min'), /原本最小值的
                        max: newmax,
                        //max: getQuantity(p, 'max'),
                        'default': value,
                        type: type,
                        store: getQuantity(p, 'stock')
                    }) && input.value == value) {
                return;
            }
            new Request({
                url: '<{link app=b2c act=update ctl=site_cart}>',
                data: p.toQueryString()+"&response_json=true",
                onComplete: function(rs){
                    rs = JSON.decode(rs);
                    console.log(rs)
                    if(rs.success) {

                        updateCart(rs, p);
                    }
                }
            }).post();
        }
        //== 更新订货单信息统一入口
        function updateCart(rs, p){
            if(!rs) return;
            if(!rs.promotion)rs.promotion = {};
            if(p) {
                updateGoodsInfo(rs.edit_ajax_data, p);
                updateGoodsPromotion(rs.promotion.goods, p);
            }
            updateOrderInfo(rs.sub_total);
            updateOrderGift(rs.order_gift);
        <{if $cart_promotion_display == 'true'}>
            updateOrderPromotion(rs.promotion.order);
            updateUnusePromotion(rs.unuse_rule);
        <{/if}>
                updateCartInfo();
            }
            //== 更新订货单价格/优惠等
            function updateGoodsInfo(rs,product) {
                if(!rs) return;
                try{
                    product.getElement('.p-integral').innerHTML = Number(rs.consume_score) || '-';
                    //这个代码是自己加的，使得销售价和小计金额同步
                    product.getElement('.p-subtotal').innerHTML = rs.mlv_price || '-';
                    product.getElement('.p-discount').innerHTML = rs.discount || '-';
                    //zeng 2017.5.8 暂时屏蔽该段代码，因为出厂价的原因导致小计金额不对
                    // product.getElement('.p-subtotal').innerHTML = rs.buy_price || '-';
                    ////
                }catch(e){}
            }
            //== 更新商品促销
            function updateGoodsPromotion(rs, product) {
                if(!rs) return;
                var tmpl = '<li><b>[{desc_tag}]</b>{name}</li>';
                container.getElements('tbody tr.cart-product').each(function(item){
                    var html = '';
                    var ident = item.getElement('input[name=goods_ident]');
                    if(!ident) return;
                    var promoarea = item.getElement('.p-promotion ul');
                    var identifier = ident.value;
                    var item = rs[identifier];
                    if(item && item.length) {
                        item.each(function(item){
                            if(item.cart_display) html += tmpl.substitute(item);
                        });
                        promoarea.innerHTML = html;
                    }
                });
            }
            //== 更新订单信息
            function updateOrderInfo(rs) {
                if(!rs) return;
                var order = $('order_price');
                if(order.getElement('.subtotal-integral')) order.getElement('.subtotal-integral').innerHTML = rs.subtotal_gain_score;
                if(order.getElement('.discount .price')) {
                    if(rs.discount_amount_order) {
                        order.getElement('.discount').show();
                        order.getElement('.discount .price').innerHTML = rs.discount_amount_order;
                    }
                    else order.getElement('.discount').hide();
                }
                order.getElement('.goods .price b').innerHTML = rs.subtotal_prefilter_after;
                order.getElement('.total .price b').innerHTML = rs.subtotal_prefilter_after;

                //add by lkb 修改购物车，不需要刷新页面再刷新积分，订单可得积分实时显示
                if(rs.subtotal_gain_score)
                {
                    order.getElement('.subtotal_gain_score').innerHTML = "(订单可得积分"+rs.subtotal_gain_score+")";
                }
                else
                {
                    order.getElement('.subtotal_gain_score').innerHTML ="";
                }
                //add end


                order.getElement('.total .price b').innerHTML = rs.promotion_subtotal;
                // 更新订单总金额存储
                // Memory.set('cart_total_price', rs.promotion_subtotal);
            }
            //== 更新订单赠品区
            function updateOrderGift(rs) {
                var giftarea = $('order_gift');
                //去掉了 赠品价格 //edit by hzk 蟹盖赠品的价格为 -
                var tmpl = '<tr class="cart-item"><td><div class="p-pic"><a href="{url}"><img src="{thumbnail}" alt="{name}"></a></div></td><td class="p-info" colspan="2"><div class="p-title"><a href="{url}">{name}</a></div><div class="p-spec">{spec_info}</div></td><td class="p-price">-</td><td class="p-num">{quantity}</td><td colspan="4"></td></tr>';
                try{
                    giftarea.getElements('.cart-item').destroy();
                }catch(e){}
                if(!rs) {
                    return giftarea.hide();
                }
                var html = '';
                var item = rs;
                if(item && item.length) {
                    item.each(function(item){
                        item.buy_price = item.price.buy_price || priceControl.format(0);
                        //赠品数量为0时不显示
                        if(item.quantity)
                        {
                            html += tmpl.substitute(item);
                        }

                    });
                }
                giftarea.show().set('html', giftarea.get('html') +html);
            }
        <{if $cart_promotion_display == 'true'}>
            //== 更新订单促销
            function updateOrderPromotion(rs) {
                var area = $('order_promotion');
                if(!rs) area.hide();
                var tmpl = '<li><b>[{desc_tag}]</b>{name}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{num_in_order_warming}</li>';
                var html = '';
                var promoarea = area.getElement('.content');
                var item = Object.values(rs);
                if(item && item.length) {
                    area.show();
                    item.each(function(item){
                        html += tmpl.substitute(item);
                    });
                }
                promoarea.innerHTML = html;
            }
            //== 更新未使用的促销
            function updateUnusePromotion(rs){
                var area = $('unused_promotion');
                if(!rs) area.hide();
                var tmpl = '<li><b>[{desc_tag}]</b>{name}</li>';
                var html = '';
                var promoarea = area.getElement('.content');
                var item = Object.values(rs);
                if(item && item.length) {
                    area.show();
                    item.each(function(item){
                        html += tmpl.substitute(item);
                    });
                };
                promoarea.innerHTML = html;
            }
        <{/if}>



                //== 赠品封顶值提示方法 add by hzk 2017-5-18
                function checkmaxbuy(e1, value,inputtype) {


                    //add by hzk 2017-5-18
                    var checkgift = e1.getParent().getElement('.action-quantity-input-havegift');
                    //alert(checkgift.value);

                    if('havegift'==checkgift.value){
                        var getmaxbuy = e1.getParent().getElement('.action-quantity-input-getmaxbuy');
                        // alert(value);
                        // alert(getmaxbuy);
                        if(!e1.hasClass('btn-decrease')){

                            if(inputtype=='button' && getmaxbuy.value == value){
                                Dialog.alert('该商品购买数量已达赠品优惠上限'+getmaxbuy.value+'，上限之后的购买数量将不赠送赠品，确定继续购买？');
                            }else if(inputtype=='text' && getmaxbuy.value <= value){
                                //alert();
                                Dialog.alert('该商品仅在前'+getmaxbuy.value+'个商品赠送赠品，上限数量之后的购买数量将不赠送赠品，确定继续购买？');
                            }
                        }
                    }

                }


                //== 为数量选择框绑定事件
                function bindQuantityEvent(elements, callback) {
                    elements = document.id(elements) || $$(elements);
                    if(!elements || !elements.length) return;
                    var value = '';
                    elements.addEvents({
                        //= 数量按钮
                        ////////////////////////////////////
                        //= edit by hzk 每次点击的（加减）数量
                        'click:relay(.btn-decrease,.btn-increase)': function(e) {
                            var input = this.getParent().getElement('input');
                            var getPeradd = this.getParent().getElement('.action-quantity-input-getPeradd');

                            value = +input.value;
                            perclicknum= parseInt(("<{$add.isused}>" ==1) ?  "<{$add.pernum}>"  : parseInt(getPeradd.value)) ;
                            var sell_num=parseInt(this.getParent("tbody").getElement("input[name='sell_num']").value);
                            var ori_price=this.getParent("tbody").getElement("input[name='ori_price']").value;

                            var _price=this.getParent("tbody").getElement(".p-price");

                            var sell_price=this.getParent("tbody").getElement("input[name='sell_price']").value;


                            checknum=value+perclicknum;
                            checkmaxbuy(this, checknum,'button');


                            peradd = this.hasClass('btn-decrease') ? (value - perclicknum) : (value + perclicknum);
                            input.value = peradd - (peradd%perclicknum);

                            console.log(input.value)
                            if(!isNaN(sell_num)){
                                if(input.value>sell_num){
                                    _price.set("text",ori_price);

                                }
                                else if(input.value<sell_num||input.value==sell_num){
                                    _price.set("text",sell_price);
                                }
                            }
                            //zeng 2017.5.7 购物车发货提示异步显示
                            var p = this.getParent('tr.p-adjunct') || this.getParent('.cart-item') || this.getParent('tr.item');
                            new Request
                            ({
                                url: '<{link app=b2c act=ajax_product_getdelivery_cart ctl=site_product}>',
                                data: p.toQueryString(),
                                onSuccess: function(rs)
                                {
                                    var str=eval("("+rs+")");//将json数据转化为数组
                                    document.getElementById('g-tips'+str['pid']).innerHTML=str['tips'];
                                }
                            }).post();

                            callback && callback(input, value);

                        },
                        ////////////////////////////////////
                        //= 数量输入框
                        'focus:relay(input)': function(e){
                            value = +this.value;
                        },
                        'change:relay(input)': function(e) {
                            callback && callback(this, value);
                        },
                        //zeng 2017.5.7 购物车发货提示异步显示
                        'input:relay(input)':function(e){
                            value = +this.value;
                            var p = this.getParent('tr.p-adjunct') || this.getParent('.cart-item') || this.getParent('tr.item');
                            //console.log(p.toQueryString());
                            new Request
                            ({
                                url: '<{link app=b2c act=ajax_product_getdelivery_cart ctl=site_product}>',
                                data: p.toQueryString(),
                                onSuccess: function(rs)
                                {
                                    var str=eval("("+rs+")");//将json数据转化为数组
                                    //alert(str);
                                    document.getElementById('g-tips'+str['pid']).innerHTML=str['tips'];
                                }
                            }).post();


                        }
                    });
                }
                bindQuantityEvent('.p-quantity', setQuantity);

                $('main').addEvents({
                        'click': function(e){
                            var el = $(e.target);

                            //==点击document隐藏删除确认框
                            var dtc = $$('.dialog-tips-container')[0];
                            var target = $(document.body).retrieve('dialog-tip_show');
                            if(dtc && dtc.retrieve('instance') && !dtc.contains(el) && target && !target.contains(el)) dtc.retrieve('instance').hide();
                        },
                        //= 移除商品
                        'click:relay(.btn-delete)': function(e) {
                            Dialog.tips(this, '确定将商品从购物车中移除吗？', function(e){
                                if(e) {
                                    var p = this.getParent('tr.p-adjunct') || this.getParent('.cart-item') || this.getParent('tr.item');
                                    new Request({
                                        url:'<{link app=b2c ctl=site_cart act=remove}>',
                                        data: p.toQueryString()+"&response_json=true",
                                        onRequest: function() {
                                            p.mask({html:'处理中...','class':'cover',injectTo:document.body,position:true,effect:false, resize: false});
                                        },
                                        onComplete:function(rs) {
                                            rs = JSON.decode(rs);
                                            if(rs.is_empty) return location.reload();
                                            p.unmask();
                                            if(rs.error) return Message.error(rs.error);
                                            if(p.hasClass('item') && $('cart_exchange').getChildren().length == 2) $('cart_exchange').destroy();
                                            else p.destroy();
                                            updateCart(rs, p);
                                        }
                                    }).post(p.get('data-product'));
                                }
                            }.bind(this));
                            document.body.store('dialog-tip_show', this);
                        },
                        //= 订单优惠收起/展开
                        'click:relay(.action-toggle)': function(e) {
                            e.stop();
                            this.set('text', this.hasClass('btn-collapse') ? '+' : '-').toggleClass('btn-collapse').toggleClass('btn-expand').getParent('h3').getNext('.content').toggle();
                        },
                        //= 凑单
                        'click:relay(.action-fororder)': function(e) {
                            e.stop();
                            var dialog = new Dialog('<{link app=b2c ctl=site_cart act=fororder}>', {
                                title: '凑单商品',
                                width: 960,
                                height: 330,
                                modal:true,
                                async: 'ajax'
                            });
                        },
                    <{if $limited_amount != null}>
                    'click:relay(.action-settle)': function(e) {
                    var order = $('order_price').getElement('.goods').getElement('.price b');
                    var limited_amount = <{$limited_amount}>;
                    var num= order.innerHTML.replace("￥","");
                    if(parseInt(num)<limited_amount){
                        Dialog.alert('订单总额不能低于'+limited_amount);
                        e.stop();
                    }

                },
            <{/if}>
                <{if $login == 'nologin' && $env.conf.b2c.site.login_type == 'target'}>
                'click:relay(.action-settle)': function(e) {
                    e.stop();
                    miniPassport('<{link app=b2c ctl=site_cart act=loginBuy}>', function(dialog){
                        this.removeClass('disabled');
                    }.bind(this));
                },
                <{/if}>
                        //= 清空订货单
                        'click:relay(.action-clean)': function(e) {
                            Dialog.confirm('确定清空购物车吗？', function(e){
                                if(e) {
                                    new Request({
                                        url:'<{link app=b2c ctl=site_cart act=remove}>',
                                        data:'obj_type=all&response_json=true',
                                        onRequest: function() {
                                            container.mask({html:'处理中...','class':'cover',position:true,effect:false, resize: false});
                                        },
                                        onSuccess:function(rs) {
                                            rs = JSON.decode(rs);
                                            container.unmask();
                                            if(rs.error) return Message.error(rs.error);
                                            location.reload();
                                        }
                                    }).post();
                                }
                            });
                        }
                    });

                //== 商品图片缩放
                if(Browser.ie6) {
                    container.getElements('.p-pic img').each(function(img){
                        img.zoomImg(80,80);
                    });
                }
            <{/if}>

                    //浏览历史
                    withBrowserStore(function(browserStore) {
                        var box = $('view_history');
                        // $('clear_history').addEvent('click', function(){browserStore.clear('history');box.hide();});
                        var myName = Cookie.read('UNAME');
                        var key = "history_"+myName;
                        browserStore.get(key, function(rs) {
                            rs = JSON.decode(rs);
                            var min = 1;
                            if (!rs || rs.length <= min) return box.hide();

                            var html = '';
                            var template = '<li class="item">';
                            template += '<div class="p-pic">';
                            template += '  <a href="<{link app=b2c ctl=site_product act=index arg0="{goodsId}"}>" target="_blank" title="{goodsName}" data-gid="{goodsId}"><img src="{goodsImg}" alt="{goodsName}" /></a>'.replace('index-', '');
                            template += '</div>';
                            template += '<div class="p-info">';
                            template += '  <h3><a href="<{link app=b2c ctl=site_product act=index arg0="{goodsId}"}>" target="_blank" title="{goodsName}">{goodsName}</a></h3>'.replace('index-', '');
                            template += '  <p class="p-price">{price}</p>';
                            template += '  <a href="<{link app=b2c ctl=site_product act=index arg0="{goodsId}"}>" target="_blank" class="btn btn-major"><span><span><{t}>立即购买<{/t}></span></span></a>'.replace('index-', '');
                            template += '</div>';
                            template += '</li>';

                            var max = Math.min(rs.length, <{$setting.max|default:8}>);
                            // document.getElement
                            // rs.length > 1 && rs.sort(function(a, b) {
                            //     return b['sortTime'] - a['sortTime'];
                            // });

                            rs.each(function(goods, index) {
                                if (index >= min && index <= max) html += template.substitute(goods);
                            });

                            box.getElement('ul').innerHTML = html;

                            box.getElements('.p-pic a').each(function(item) {
                                var img = item.getElement('img');
                                img.zoomImg(96,96);
                                img.onerror = function(){
                                    imgRequest(item, img, rs, min, max);
                                }
                                if (!img.get('src')) {
                                    imgRequest(item, img, rs, min, max);
                                }
                            });
                        });

                        function imgRequest(el, img, rs, min, max) {
                            var gid = el.get('data-gid');
                            new Request.JSON({
                                url: "<{link app=b2c ctl=site_product act=picsJson}>",
                                onComplete: function(data) {
                                    new Asset.image(data, {
                                        onload: function() {
                                            img.src = data;
                                            img.zoomImg(96,96);
                                        },
                                        onerror: function() {
                                            el.set('text', '<{t}>图片加载失败<{/t}>');
                                        }
                                    });

                                    rs.map(function(goods, index) {
                                        if (index >= min && index <= max && goods['goodsId'] == gid) return goods['goodsImg'] = data;
                                    });
                                    // var myName = Cookie.read('UNAME');
                                    // var key = 'history_'+myName;
                                    browserStore.set(key, JSON.encode(rs));
                                }
                            }).get({'gids': gid});
                        }
                    });
                })();
</script>
